package com.joysuch.wwyt.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * 数据源工具
 *
 * @Author ZhangDong
 * @Date 2020/9/29 9:52
 */
public class JdbcTemplateFactory {

    public static NamedParameterJdbcTemplate getJdbcTemplate(String url, String username, String password) {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(1);
        dataSource.setMinIdle(1);
        dataSource.setMaxIdle(2);
        dataSource.setMaxActive(5);
        dataSource.setRemoveAbandoned(true);
        dataSource.setRemoveAbandonedTimeout(180);
        dataSource.setMaxWait(60 * 1000);
        dataSource.setTimeBetweenEvictionRunsMillis(60 * 1000);
        dataSource.setValidationQuery("SELECT 1 FROM DUAL");
        dataSource.setTestWhileIdle(true);
        return new NamedParameterJdbcTemplate(dataSource);
    }

}
